package proj2501.geo_util;

public class VectorD2 {
	private double x;
	public double getX() { return x; }
	
	private double y;
	public double getY() { return y; }

	public VectorD2(double x, double y) {
		this.x = x;
		this.y = y;
	}
	
	@Override
	public String toString() {
		return "(" + x + ", " +  y + ")";
	}
	
	private boolean hashCodeInitDone;
	private int hashCode;
	
	@Override
	public int hashCode() {
		if (hashCodeInitDone) {
			return hashCode;
		}
		hashCode = new Double(x).hashCode();
		hashCode <<= 1;
		hashCode += new Double(y).hashCode();
		return hashCode;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof VectorD2) {
			VectorD2 v = (VectorD2) obj;
			if (x == v.x && y == v.y) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	public double getLength() {
		return Math.sqrt(x * x + y * y);
	}
	
	public VectorD2 normalize() {
		double length = getLength();
		return new VectorD2(x / length, y / length);
	}
	
	public VectorD2 add(VectorD2 v) {
		return new VectorD2(x + v.x, y + v.y);
	}
	
	public VectorD2 sub(VectorD2 subtrahend) {
		return new VectorD2(x - subtrahend.x, y - subtrahend.y);
	}
	
	public VectorD2 mul(double scalar) {
		return new VectorD2(x * scalar, y * scalar);
	}
	
	public VectorD2 div(double scalar) {
		return new VectorD2(x / scalar, y / scalar);
	}
	
	public double dot(VectorD2 v) {
		return x * v.x + y * v.y;
	}
	
	public double cross(VectorD2 v) {
		return x * v.y - y * v.x; 
	}
	
	public static VectorD2 getXVector() {
		return new VectorD2(1, 0);
	}
	
	public static VectorD2 getYVector() {
		return new VectorD2(0, 1);
	}
}
